package com.limh.jkzs.fragment.blue

import android.text.TextUtils
import android.view.View
import android.widget.*
import com.limh.blehelper.Ble2Manager
import com.limh.blehelper.utils.DataUtils
import com.limh.jkzs.R
import com.limh.jkzs.adapter.CommAdapter
import com.limh.jkzs.base.BaseFragment
import com.limh.jkzs.dao.BlueValue
import com.limh.jkzs.util.CacheUtils
import com.limh.jkzs.util.GsonUtils
import com.limh.jkzs.util.Logs
import com.limh.jkzs.widget.InputDialog
import com.limh.jkzs.widget.LineGridView
import com.limh.jkzs.widget.RockerView

/**
 * Function:
 * author: limh
 * time:2018/1/17
 */
class ControlFragment : BaseFragment(), SeekBar.OnSeekBarChangeListener {

    private lateinit var lineGrid: LineGridView

    private lateinit var seekP: SeekBar
    private lateinit var seekI: SeekBar
    private lateinit var seekD: SeekBar

    private lateinit var txtP: TextView
    private lateinit var txtI: TextView
    private lateinit var txtD: TextView
    private lateinit var txtValue: TextView

    private lateinit var radio: RadioGroup
    private lateinit var radio1: RadioButton
    private lateinit var radio2: RadioButton
    private lateinit var rockerView: RockerView


    private val datas = arrayListOf<BlueValue>()
    private var adapter: CommAdapter<BlueValue>? = null

    override fun findView(): Int {
        return R.layout.fragment_control
    }

    override fun initView(view: View) {
        lineGrid = view.findViewById(R.id.lineGrid)
        seekP = view.findViewById(R.id.seekP)
        txtP = view.findViewById(R.id.txtP)
        seekI = view.findViewById(R.id.seekI)
        txtI = view.findViewById(R.id.txtI)
        seekD = view.findViewById(R.id.seekD)
        txtD = view.findViewById(R.id.txtD)
        radio = view.findViewById(R.id.radio)
        radio1 = view.findViewById(R.id.radio1)
        radio2 = view.findViewById(R.id.radio2)
        rockerView = view.findViewById(R.id.rockerView)
        txtValue = view.findViewById(R.id.txtValue)

        if (TextUtils.isEmpty(CacheUtils.get(this.context!!, "blue_key", "") as String)) {
            (1..12).mapTo(datas) { BlueValue("自定义" + it, "" + it) }
        } else {
            datas.addAll(GsonUtils.str2List(CacheUtils.get(this.context!!, "blue_key", "[]") as String, BlueValue::class.java))
        }
        adapter = object : CommAdapter<BlueValue>(datas, R.layout.view_iten_blue_key) {
            override fun bindView(holder: ViewHolder?, obj: BlueValue?) {
                holder!!.setText(R.id.txt_blue_key, obj!!.keyName)
            }
        }
        lineGrid.adapter = adapter
        lineGrid.setOnItemClickListener { _, _, i, _ ->
            txtValue.text = String.format("键值：%s", datas[i].value)
            Ble2Manager.getInstance().sendByHex(datas[i].value.toByteArray())
        }
        lineGrid.setOnItemLongClickListener { _, _, i, _ ->
            showDialog(i)
            return@setOnItemLongClickListener true
        }
        radio.setOnCheckedChangeListener { radioGroup, i ->
            when (radioGroup.checkedRadioButtonId) {
                R.id.radio1 -> {
                    txtValue.text = "键值："
                    lineGrid.visibility = View.VISIBLE
                    rockerView.visibility = View.GONE
                }
                R.id.radio2 -> {
                    txtValue.text = "角度："
                    lineGrid.visibility = View.GONE
                    rockerView.visibility = View.VISIBLE
                }
            }
        }
        seekP.setOnSeekBarChangeListener(this)
        seekI.setOnSeekBarChangeListener(this)
        seekD.setOnSeekBarChangeListener(this)

        rockerView.setOnAngleChangeListener(object : RockerView.OnAngleChangeListener {
            override fun onStart() {
                txtValue.text = "角度："
            }

            override fun angle(angle: Double) {
                Logs.d(TAG, "当前角度=" + angle)
                Ble2Manager.getInstance().sendByStr("" + angle)
                txtValue.text = String.format("角度：%s", angle)
            }

            override fun onFinish() {
                txtValue.text = "角度："
            }
        })
    }

    override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
        when (p0!!.id) {
            R.id.seekP -> {
                txtP.text = String.format("P%d", p1)
            }
            R.id.seekI -> {
                txtI.text = String.format("I%d", p1)
            }
            R.id.seekD -> {
                txtD.text = String.format("D%d", p1)
            }
        }
        Ble2Manager.getInstance().sendByHex(p1.toString().toByteArray())
    }

    override fun onStartTrackingTouch(p0: SeekBar?) {

    }

    override fun onStopTrackingTouch(p0: SeekBar?) {

    }

    private fun showDialog(position: Int) {
        val dialog = InputDialog(this.context!!)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setContentView(R.layout.view_set_key_value)
        val editName = dialog.findViewById<EditText>(R.id.edit_dialog_name)
        val editValue = dialog.findViewById<EditText>(R.id.edit_dialog_value)
        dialog.findViewById<TextView>(R.id.txt_dialog_cancel).setOnClickListener {
            if (dialog.isShowing)
                dialog.dismiss()
        }
        dialog.findViewById<TextView>(R.id.txt_dialog_yes).setOnClickListener {
            if (TextUtils.isEmpty(editName.text.toString()) || TextUtils.isEmpty(editValue.text.toString())) {
                showShort("输入项不能为空")
            } else {
                datas[position].keyName = editName.text.toString().trim()
                datas[position].value = editValue.text.toString().trim()
                adapter!!.setData(datas)
                CacheUtils.put(context!!, "blue_key", GsonUtils.list2Str(datas))
                if (dialog.isShowing)
                    dialog.dismiss()
            }
        }
        dialog.show()
    }

}